<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>采购需求 - 产品数据管理系统</title>
{include file="demand_header.php"}
<style>
{literal}
.box-body{min-height:350px;}
.table-toolbar .columns-right{position:relative;}
.table-toolbar .dropdown-menu{right:0;left:auto;max-height:340px;overflow:auto;}
.table-toolbar .dropdown-menu li{padding:5px 15px;}
.table-toolbar .dropdown-menu li p{margin:0;}
.img-thumbnail{width:50px;height:50px;}
.nav-tabs-custom{box-shadow:none;margin:10px 10px 0px 10px;}
.slct{display:inline-block; width:auto;}
{/literal}
</style>
</head>
<body class="skin-blue">
<div class="wrapper">
	{include file="bread_crumbs.php"}
	<!--引入面包屑-->
	<section class="base-setting-wrap">
		<div class="row">
			<div class="col-md-12">
				<div class="box">
					<div class="nav-tabs-custom">
						<ul class="nav nav-tabs">
							<li class="active"><a href="/backup/demand">采购需求列表</a></li>
							<li><a href="/backup/demand/reject-list">需求待确认列表</a></li>
							<li><a href="/backup/demand/story-intercept">需求拦截列表</a></li>
							<li><a href="/backup/log/list">采购需求产生日志</a></li>
						</ul>
					</div>
	                <div class="box-header with-border clearfix">
	                	<div class="table-toolbar pull-right mb10 clearfix">
						  <div class="columns-right btn-group pull-right">
						    <button class="btn btn-default btn-sm" type="button" title="刷新" onClick="window.location.reload();"><i class="glyphicon glyphicon-refresh icon-refresh"></i></button>
						    <div class="keep-open btn-group" title="列">
						      <button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><i class="glyphicon glyphicon-th icon-th"></i> <span class="caret"></span></button>
						        <ul class="dropdown-menu" id="js_productOption">
								</ul>
						    </div>
						  </div>
						</div>
	                	<div class="box-form pull-right form-group-sm mr20">
	                		<p class="pull-right"><a href="javascript:;" class="btn btn-success btn-sm" id="js_toggleSearchBox"><i class="fa fa-search"></i><span class="pl5">搜索选项</span></a></p>
	                	</div>
	                	<div class="clearfix"></div>
						<div class="search-box" id="js_searchBox">
							<form method="get" action="/backup/demand" class="form-inline" id="demand">
								<div class="form-group-sm">
									<p>
										<label class="control-label">产品分类：</label>
										<select id="cat_id" data-path='{$queryParameter['catalogPath']}' style="font-size:12px;font-family:;microsoft YaHei;"></select>
										<input type="hidden" name="catalog_id" id="select_cat_id"/>&nbsp;
										<label class="control-label">产生时间：</label>
										<input type="text" name="create_date[]" id="date_start" class="form-control js_datePicker date_start">到
										<input type="text" name="create_date[]" id="date_end" class="form-control js_datePicker date_end">
										<label class="control-label">所属事业部：</label>
										<select class="form-control" id="dept_id" name="dept_id">
											<option value="">选择事业部</option>
											{$depts}
										</select>
										<select class="form-control ml10" name="warehouse_code" id="warehouse_code" style="width:200px;">
											<option value="">选择仓库</option>
										</select>
										<div style="clear:both"></div>
										<label class="control-label">可用库存：</label>
										<input type="text" class="form-control" name="stock_from">到
										<input type="text" class="form-control" name="stock_to">
										<label class="control-label">审核状态：</label>
										<select class="form-control" name="examine_status" id="examine_status">
											<option value="">所有</option>
											<option value="1">待小组长审核</option>
											<option value="2">待大组长审核</option>
											<option value="3">待主管审核</option>
											<option value="4">待经理审核</option>
											<option value="5">审核通过</option>
											<option value="6">小组长驳回</option>
											<option value="7">大组长驳回</option>
											<option value="8">主管驳回</option>
											<option value="9">经理驳回</option>
											<option value="10">已作废</option>
										</select>
										<label class="control-label">传输状态：</label>
										<select class="form-control" name="transfer_status" id="transfer_status">
											<option value="">请选择</option>
											<option value="1">未传输</option>
											<option value="2">已传输</option>
										</select>
										<label class="control-label ml10">搜索关键字类别：</label>
										<select class="form-control" name="search_type" id="search_type">
			                				<option value="">请选择</option>
			                				<option value="1" selected>产品编码</option>
			                				<option value="2">产品名称</option>
			                				<option value="3">采购员</option>
		                				</select>
										<input type="text" class="form-control" name="search">
										<button type="button" class="btn btn-primary btn-sm" id='demand_search'>立即搜索</button>
<!-- 										<button class="btn btn-primary btn-sm">导出</button> -->
										<a href="/backup/demand/">清空搜索条件</a>
									</p>
								</div>
							</form>
						</div>
	                </div>
	                <!-- /.box-header -->
					<form action="/demand/index/examine" method="post">
		                <div class="box-body">
		                  <table class="table table-bordered table-striped table-hover" id="js_productTable">
		                  	<thead>
		                    <tr>
		                      <th><input name="id[]" type="checkbox"></th>
		                      <th data-field="goods_sn" data-checked="{if in_array('goods_sn', $productTableFiled)}true{else}false{/if}">产品编码</th>
		                      <th data-field="thumb_url" data-checked="{if in_array('thumb_url', $productTableFiled)}true{else}false{/if}">产品图片</th>
		                      <th data-field="product_name" data-checked="{if in_array('product_name', $productTableFiled)}true{else}false{/if}" width="280">产品名称</th>
		                      <th data-field="purchase_price" data-checked="{if in_array('purchase_price', $productTableFiled)}true{else}false{/if}" width="85">采购价</th>
		                      <th data-field="stock_can_allot" data-checked="{if in_array('stock_can_allot', $productTableFiled)}true{else}false{/if}">可用库存</th>
		                      <th data-field="sale_count" data-checked="{if in_array('sale_count', $productTableFiled)}true{else}false{/if}">备货销量</th>
		                      <th data-field="actual_count" data-checked="{if in_array('actual_count', $productTableFiled)}true{else}false{/if}">实际销量</th>
		                      <th data-field="sale_warning" data-checked="{if in_array('sale_warning', $productTableFiled)}true{else}false{/if}">销售预警</th>
		                      <th data-field="a_story_quantity" data-checked="{if in_array('a_story_quantity', $productTableFiled)}true{else}false{/if}" width="60">需求A</th>
		                      <th data-field="b_story_quantity" data-checked="{if in_array('b_story_quantity', $productTableFiled)}true{else}false{/if}" width="60">需求B</th>
		                      <th data-field="c_story_quantity" data-checked="{if in_array('c_story_quantity', $productTableFiled)}true{else}false{/if}" width="60">需求C</th>
		                      <th data-field="story_quantity_total" data-checked="{if in_array('story_quantity_total', $productTableFiled)}true{else}false{/if}">需求总数</th>
		                      <th data-field="purchaser" data-checked="{if in_array('purchaser', $productTableFiled)}true{else}false{/if}">采购员</th>
		                      <th data-field="life_cycle" data-checked="{if in_array('life_cycle', $productTableFiled)}true{else}false{/if}">生命周期</th>
		                      <th data-field="product_status" data-checked="{if in_array('product_status', $productTableFiled)}true{else}false{/if}">产品状态</th>
		                      <th data-field="warehouse_code" data-checked="{if in_array('warehouse_code', $productTableFiled)}true{else}false{/if}">存放仓库</th>
		                      <th data-field="platform_warehouse" data-checked="{if in_array('platform_warehouse', $productTableFiled)}true{else}false{/if}">平台仓</th>
		                      <th data-field="promote_status" data-checked="{if in_array('promote_status', $productTableFiled)}true{else}false{/if}">存销比</th>
		                      <th data-field="stocking_type" data-checked="{if in_array('stocking_type', $productTableFiled)}true{else}false{/if}">备货类型</th>
		                      <th data-field="delivery_info" data-checked="{if in_array('delivery_info', $productTableFiled)}true{else}false{/if}">交期信息</th>
		                      <th data-field="shelf_time" data-checked="{if in_array('shelf_time', $productTableFiled)}true{else}false{/if}">上架时间</th>
		                      <th data-field="examine_status" data-checked="{if in_array('examine_status', $productTableFiled)}true{else}false{/if}">审核状态</th>
		                      <th data-field="transfer_status" data-checked="{if in_array('transfer_status', $productTableFiled)}true{else}false{/if}">传输状态</th>
		                      <th data-field="add_time" data-checked="{if in_array('add_time', $productTableFiled)}true{else}false{/if}">产生时间</th>
		                    </tr>
		                    </thead>
		                    {foreach from=$p item="vor"}
		                    <tr>
								<td><input type="checkbox" name="checkitem" value="{$vor.id}"></td>
								<td>{$vor.goods_sn}</td>
								<td><img src="{$pdmDomain}{$vor.thumb_url}" alt=""></td>
								<td>{$vor.product_name}</td>
								<td>{$vor.purchase_price}</td>
								<td>{$vor.base_stock}</td>
								<td>
							      {foreach from=$sale_day  key='_statistics_type' item='_statistics_name'}
											{$_statistics_name}：{$vor.sales_.{$_statistics_type}|default:0}<br />
								  {/foreach}
								</td>
								<td width="140">
								    {foreach from=$sale_day  key='_statistics_type' item='_statistics_name'}
											{$_statistics_name}：{$vor.sales.{$_statistics_type}|default:0}<br />
								    {/foreach}
								</td>
								<td>{$vor.sale_warning}</td>
								<td class="abc_" {if $vor.a_story_quantity_modify_log!=""} style="color:red"{/if} data-modify-log="{$vor.a_story_quantity_modify_log}">{$vor.a_story_quantity}</td>
								<td class="edit abc_ modify_quantity" date-sku="{$vor.goods_sn}" data-warehousecode="{$vor.warehouse_code}" data-amount="{$vor.b_story_quantity}" data-story-type="b" data-story-id="{$vor.id}" {if $vor.b_story_quantity_modify_log!=""} style="color:red"{/if} data-modify-log="{$vor.b_story_quantity_modify_log}" >{$vor.b_story_quantity}</td>
								<td class="edit abc_ modify_quantity" data-amount="{$vor.c_story_quantity}" data-story-type="c" data-story-id="{$vor.id}" {if $vor.c_story_quantity_modify_log!=""} style="color:red"{/if} data-modify-log="{$vor.c_story_quantity_modify_log}">{$vor.c_story_quantity}</td>
								<td class="abc">{math equation='x + y + z' x=$vor.a_story_quantity y=$vor.b_story_quantity z=$vor.c_story_quantity}</td>
								<td>{$vor.purchaser}</td>
								<td>{$vor.life_cycle}</td>
								<td>{$pstatus[$vor['product_status']]}</td>
								<td>{$warehouse[$vor.warehouse_code]['warehouse_name']}</td>
								<td>{if $vor.platform_warehouse}{$warehouse[$vor.platform_warehouse]['warehouse_name']}{else}-{/if}</td>
								<td>{$vor.sale_stock_pre}</td>
								<td>{$vor.stocking_type_char}</td>
								<td>{if empty($vor.delivery_info)}-{else}{$vor.delivery_info}{/if}</td>
								<td>{$vor.shelf_time}</td>
								<td class="story_examine_status" data-examine-log='{$vor.storyExamineLog}'>
									{if !isset($examine[$vor['examine_status']])}
										-
									{else}
										{$examine[$vor['examine_status']]}
									{/if}
								</td>
								<td>{$transferStatus[$vor['transfer_status']]}</td>
								<td>{$vor['add_time']}</td>
							</tr>
							{/foreach}
		              	</table>
		              	<div class="form-group-sm mt20">
		              		<select class="form-control slct" name="examine" id="checkOperationType" >
		              		    <option value="1">小组长审核通过</option>
		              			<option value="2">大组长审核通过</option>
		              			<option value="3">主管审核通过</option>
		                        <option value="4">经理审核通过</option>
		              			<option value="5">小组长驳回</option>
		              		    <option value="6">大组长驳回</option>
		              			<option value="7">主管驳回</option>
		              			<option value="8">经理驳回</option>
		              		</select>
		              		<input type="button" class="btn btn-primary btn-sm" id="betchCheckStroy" value="确定"></input>
		              	</div>
		                </div>
		                <!-- .box-body -->
	                </form>
	                <div class="box-footer text-center clearfix">{$page}</div>
	              </div>
			</div>
		</div>
	</section>
</div>
<script src="{$js.url}lib/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="{$js.url}lib/bootstrap.min.js" type="text/javascript"></script>
<script src="{$js.url}plugins/datapicker/bootstrap-datepicker-cn.js" type="text/javascript"></script>
<script src="{$js.url}plugins/multiselect/js/bootstrap-multiselect.js" type="text/javascript"></script>
<script src="{$js.url}plugins/layer.js" type="text/javascript"></script>
<script src="{$js.url}plugins/zeroclipboard/ZeroClipboard.min.js" type="text/javascript"></script>
<script src="{$js.url}linkagesel-min.js" type="text/javascript"></script>
<script src="{$js.url}plugins/jquery.ztree.all-3.5.min.js" type="text/javascript"></script>
<!-- 日期 -->
<script src="{$js.url}plugins/bootstrap-datetimepicker.min.js" type="text/javascript"></script>
<!--common js-->
<script src="{$js.url}common.js"></script>
<link rel="stylesheet" type="text/css" href="{$js.url}plugins/select2/css/select2.min.css">
<script type="text/javascript" src="{$js.url}plugins/select2/js/select2.min.js"></script>
<script>
var data = {$data};
{literal}
var productTableFiled = new Array();
//获取勾选显示的字段

$('[data-checked="true"]').each(function(i){
	field = $(this).attr('data-field');
	productTableFiled.push(field);
});

$.createTableOption({
	tableBox:"#js_productTable",
	optionBox:"#js_productOption",
	onOptionChange:function(index,obj,field){
		//保存cookie
		if(obj.prop("checked")){
			productTableFiled.push(field);
		}
		else{
			productTableFiled.remove(field);
		}
		setFiledCookie();
	}
});
// 存储 cookie
function setFiledCookie(){
	$.cookie('productTableFiled',productTableFiled.join(","),{expires: 7, path:'/'});
}
setFiledCookie();


$('#dept_id option').each(function (){
	if($(this).val() == data['dept_id']){
		$(this).prop('selected', 'selected');
	}
});
$.each($('#sale_type option'), function (){
	if($(this).val() == data['sale_type']){
		$(this).prop('selected', 'selected');
	}
});
$.each($('#transfer_status option'), function (){
	if($(this).val() == data['transfer_status']){
		$(this).prop('selected', 'selected');
	}
});
$('#warehouse_code').each(function (){
	if($(this).val() == data['warehouse_code']){
		$(this).prop('selected', 'selected');
	}
});
$('#search_type option').each(function (){
	if($(this).val() == data['search_type']){
		$(this).prop('selected', 'selected');
	}
});
if(data['create_date']){
	$('#date_start').val(data['create_date'][0]);
	$('#date_end').val(data['create_date'][1]);
}
$('input[name="search"]').val(data.search);
$('input[name="stock_from"]').val(data['stock_from']);
$('input[name="stock_to"]').val(data['stock_to']);
if(data['actual_count']){
	$('#sale_from').val(data['actual_count'][0]);
	$('#sale_to').val(data['actual_count'][1]);
}
$('#examine_status option').each(function (){
	if($(this).val() == data.examine_status){
		$(this).prop('selected', 'selected');
	}
});
function warehouse(did){
	var did = !isNaN(did) ? did : $(this).find('option:selected').val();
	var str = '<option value="0">选择仓库</option>';
	$.ajax({
		url : '/base/warehouse/getselectedwarehousebydeptid',
		data : {did : did},
		dataType : 'json',
		type : 'get',
		success : function (result){
			$.each(result, function(i, n){
				if(n.warehouse_code == data['warehouse_code']){
					str += '<option value="' + n.warehouse_code + '" selected>' + n.warehouse_name + '</option>';
				}else{
					str += '<option value="' + n.warehouse_code + '">' + n.warehouse_name + '</option>';
				}
				//str += '<option value="' + n.warehouse_code + '">' + n.warehouse_name + '</option>';
			});
			$('select[name="warehouse_code"]').html(str);
			$("#warehouse_code").select2();
		}
	});
}
//全选反选
$.fn.check({checkall_name: "id[]", checkbox_name:"checkitem"});
// 日期选择
$('.date_start').datetimepicker({
    format: "yyyy-mm-dd",
    minView:2,
    autoclose: true,
    todayHighlight: false
}).on('changeDate', function(ev){
    $(this).next('.date_end').val('').datetimepicker('setStartDate',$(this).val());
});
$('.date_end').datetimepicker({
    format: "yyyy-mm-dd",
    minView:2,
    autoclose: true,
    todayHighlight: false
});
warehouse(data.dept_id);
$('#dept_id').change(warehouse);

//产品分类
var catalogPath=$("#cat_id").attr("data-path");
var opts = {
	ajax: '/widget/category/getcat',
	selClass:'form-control inline',
	selStyle: 'font-size:12px;font-family:;microsoft YaHei;',
	autoLink : false,
	defVal:catalogPath.split(','),
	head: '请选择产品分类',
	select:  '#cat_id',
};
var linkageSel = new LinkageSel(opts);
$('#demand_search').click(function(){
	var v = linkageSel.getSelectedValue();
	$('#select_cat_id').val(v);
	$("#demand").submit();
});

//需求审核
$("#betchCheckStroy").bind("click", function() {
	var ids = [];
	$("input[name=checkitem]").each(function(index,node){
		if ($(node).prop('checked')) {
			ids.push($(node).val());
		};
	})
	if (ids.length < 1) {
		layer.alert("请选择要审核或者驳回的需求！");
		return false;
	};
    var checkOperationType=$("#checkOperationType").val();
    if(checkOperationType==0){
    	layer.alert("请选择操作类型！");
		return false;
    }
    var operationName=$("#checkOperationType option:selected").text();
    var checkUrl="/backup/demand/check";
    if(!checkOperationType){
    	return false;
    }
	layer.confirm("确定要"+operationName+"这些需求？", function(){
		$.post(checkUrl, {story_id:ids.join(','),operation_type:checkOperationType}, function(res) {
			if(res.success == true){
                layer.alert(res.msg);
                location.reload();
            }else if(res.success == false){
                layer.alert(res.msg);
            }
		}, 'json');
	});
})


//编辑备货数量
$(function (){
	$('.modify_quantity').dblclick(function (){
		clicked = $(this);
		var story_examine_status=clicked.parent().find('.story_examine_status').text();
        if($.trim(story_examine_status)=='审核通过'){
        	layer.alert("需求当前状态不允许修改数量！");
        	return false
        }

		var value = clicked.text();
		if(value==0){
			layer.alert("当前需求数量为零，不允许修改数量！");
        	return false
		}
		var dblclick = $(this);
		inputInit();
		$(this).html('<input type="text" style="width:40px;" class="input" value="' + value + '" />');
		$('.input').blur(function (){
			var changeResult=changeInputForText($(this));
			if(changeResult==false){
			   dblclick.remove('input');
			   dblclick.text(value);
			}
		});
		$('input.input').click(function (){
			return false;
		});
		$('input.input').dblclick(function (){
			return false;
		});
		$('.input').keypress(function (event){
			if(event.keyCode == 13){
				var changeResult=changeInputForText($(this));
				if(changeResult==false){
			       dblclick.remove('input');
			       dblclick.text(value);
			   }
				return false;
			}
		});
		function inputInit(){
			$('.modify_quantity').each(function (){
				$(this).html($(this).find('input').val());
			});
			$('.modify_quantity input').remove();
		}
		function changeInputForText(obj){
			if(isNaN(obj.val())){
				layer.msg('错误：请只输入阿拉伯数字！');
				return false;
			}
			if(obj.val()<0){
	            layer.alert('不能小于0！');
				return false;
			}

		    if(obj.val()>value*(1+0.3)){
	            layer.alert('不能增加超过原值的30%！');
				return false;
			}

			var editedNum = clicked.parent().find('.abc_');
			var abcAmount = 0;
			dblclick.remove('input');
			dblclick.text(obj.val());
			$.each(editedNum, function (){
				abcAmount += parseInt($(this).text());
			});
			clicked.parent().find('.abc_story_amount').text(abcAmount);

			layer.confirm("确定要修改需求的数量？", function(){
                    sendEdit();
			},function(){
				   dblclick.remove('input');
			       dblclick.text(value);
			});
			return true
		}

		// 修改确认后的操作
		function sendEdit(){
			var story_id=clicked.data('story-id');
			var story_type=clicked.data('story-type');
            var quantity=clicked.text()
		   	$.ajax({
				   type: "get",
				   url: '/backup/demand/modify-quantity',
				   data: {story_id:story_id,story_type:story_type,quantity:quantity},
				   success: function(result){
				     layer.msg( result.msg );
		              if(result.success){
				       location.reload();
		              }
				   }
		    });

		}
	});
})

//需求数量更改日志
$("td.abc_").hover(
  function () {
	var log = $(this).data('modify-log');
	if(log != ''){
		layer.tips(log, this, {
			skin: 'layui-layer-molv',
			tips: [4, '#3595CC'],
			time:0
		});
	}
  },
  function () {
   layer.closeAll('tips')
  }
);

//需求审核日志
$("td.story_examine_status").hover(
  function () {
	var log = $(this).data('examine-log');
	if(log != ''){
		layer.tips(log, this, {
			skin: 'layui-layer-molv',
			tips: [4, '#3595CC'],
			time:0,
			area: ['300px', 'auto']
		});
	}
  },
  function () {
   layer.closeAll('tips')
  }
);

{/literal}
</script>
</body>
</html>
